/* ====================================== toast  ====================================== */
import React from 'react'
// ===================================================================== DatePicker
import { DatePicker } from 'antd'
import $time from '@utils/moment'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')
// ===================================================================== declare
const { $fn } = window
// ===================================================================== DatePicker
class Index extends React.Component{
	state = {}
	
	myType = [ 'full', 'ymd', 'ymd-hm', 'ym', 'yyyy', 'ym' ]

	onChange = value => {
		const { onChange, onChanged, name, range, format = 0, outer } = this.props
		const formatType = this.myType[format]
		// const formatType = format === 1 ? 'ymd' : 'full'
		
		const $format = value => $time.format(value, { t: formatType }) 	// 将时间格式化为字符串
		
		if(!outer){ this.setState({ value }) }
		
		let model = null
		if(range){
			let start = null
			let end = null
			if($fn.isArray(value)){
				start = $format(value[0])
				end = $format(value[1])
				model = $fn.isArray(name) ? { [name[0]]:start, [name[1]]: end } : { start, end }
			}else{
				model = $fn.isArray(name) ? { [name[0]]:'', [name[1]]: '' } : { start:'', end:'' }
			}
			onChanged?.({ model, start, end, name })
			onChange?.([start, end])
		}else{
			let time = $format(value)
			model = name ? { [name]: time } : time
			onChanged?.({ model, value: time, name })
			onChange?.(time)
		}
	}
	
	setValue = value =>  this.setState({ value: value})
	
	clear = () => this.setState({ value: '' })
	
	disabledBefore = current =>{
		let { before, today = true } = this.props
		const _bool = Date.now() - (today ? 8.64e7 : 0)
		const _str = moment(before) - (today ? 0 : 8.64e7)
		if(before){
			const v = typeof before === 'string' ? _str : _bool
			return current && (current < v)
		}else{
			return null
		}
	}
	disabledAfter = current =>{
		const { after, today = true, value, search, submit } = this.props
		const _bool = Date.now() - (today ? 0 : 8.64e7)						// 包含今天
		let _str = moment(after)
		
		if(!submit){
			if(!value){ _str += 8.64e7 }
		}
		
		// 用于时间段搜索禁用时
		if(search){
			if(value){ _str += 8.64e7 }
		}
		
		if(after){
			const time = typeof after === 'string' ? _str : _bool
			return current && (current > time)
		}else{
			return null
		}
	}
	disabledDate = current => {
		const { before, after, picker} = this.props
		if (before && after) {
			return current && (current.isBefore(before) || current.isAfter(after, picker ? picker : null))
		} else if (before) {
			return this.disabledBefore(current)
		} else if (after) {
			return this.disabledAfter(current)
		} else {
			return null
		}
	}
	
	onBefore = (before, index) => {
		let stack = []
		if($fn.hasArray(before)){
			let t = +before[index]
			for(var i=0; i<t; i++){
				stack.push(i)
			}
		}
		return stack
	}
	
	onAfter = (after, index) => {
		let stack = []
		if($fn.hasArray(after)){
			const s = [ 23, 59, 59 ]
			let t = +after[index]
			for(var i=s[index]; i>t; i--){
				stack.push(i)
			}
		}
		return stack
	}
	
	render(){
		const { after, before, width, size, className, disabledTime, range, p, disabled, format = 0, value, bordered, defaultPickerValue, defaultToday, showTimeStart, showTimeEnd, picker, showToday = true, tabIndex} = this.props
		let formatType = ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD', 'YYYY-MM-DD HH:mm', 'YYYY-MM', 'YYYY 年', 'YYYYMM']
		// let myType = [ 'full', 'ymd', 'ymd-hm', 'ym' ]
		formatType = formatType[format]
		
		let _value = this.state.value === undefined ? value : this.state.value
		
		if($fn.isArray(_value) && range){
			_value = [
				_value[0] === null ? null : moment(_value[0], 
				formatType), _value[1] === null ? null : moment(_value[1], formatType)
			]
		}else{
			_value = _value ? moment(_value, formatType) : null
			if(!_value && defaultToday){
				_value = moment( $time.format(Date.now(), { t: this.myType[format] }), formatType)
			}
		}
		
		let _showTime =  formatType.indexOf('HH') >= 0
		
		if(_showTime){
			if(showTimeStart){
				_showTime = { defaultValue: moment('00:00:00', 'HH:mm:ss') }
			}else if(showTimeEnd){
				_showTime = { defaultValue: moment('23:59:59', 'HH:mm:ss') }
			}
		}
		const Picker = range ? DatePicker.RangePicker : DatePicker
		
		let time
		if(disabledTime){
			$time.format(Date.now(), { t: 'hms' })
			time = time.split(':')
		}
		return (
			<Picker
				defaultPickerValue = { defaultPickerValue ? moment(defaultPickerValue, formatType) : null }
				value		= { _value } 
				size		= { size || 'small' } 
				format  	= { formatType }
				onChange	= { this.onChange }
				showTime	= { _showTime  }
				tabIndex	= { tabIndex }
				style		= {{ width }} 
				className 	= {`${className?className:''} ${bordered===false ? 'input-bordered':''}`}
				placeholder = { p } 
				disabledDate = { this.disabledDate }
				disabledTime = { disabledTime ? ()=>{
					return {
						disabledHours: () => {
							if(after){
								return this.onAfter(time,0)
							}else if(before){
								return this.onBefore(time,0)
							}
						},
						disabledMinutes: () => {
							if(after){
								return this.onAfter(time,1)
							}else if(before){
								return this.onBefore(time,1)
							}
						},
						disabledSeconds: () => {
							if(after){
								return this.onAfter(time,2)
							}else if(before){
								return this.onBefore(time,2)
							}
						},
					 }
				} : null}
				// disabledDate = { (before ? this.disabledBefore : (after ? this.disabledAfter : null)) }
				disabled 	= { disabled }
				bordered	= { bordered }
				allowEmpty	= { range ? [true, true] : null }
                picker = { picker }
				showToday	= { showToday }
			/>
		)
	}
}

export default Index